package client.comm.baoding.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.base.BaseAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.baoding.R
import client.comm.baoding.databinding.LayoutAddrmangerItemBinding
import client.comm.baoding.api.bean.UserAddress


class AddrMangerAdapter : BaseAdapter<UserAddress.Address> {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = BindingViewHoder(
            DataBindingUtil.inflate<ViewDataBinding>(
                layoutInflater,
                R.layout.layout_addrmanger_item, parent, false
            )
        )

        return holder
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>

        val layoutAddrmangerItemBinding = bindingViewHolder.binding as LayoutAddrmangerItemBinding
        val bean = getItem(position) as UserAddress.Address
        layoutAddrmangerItemBinding.e = bean
        layoutAddrmangerItemBinding.event = this
        layoutAddrmangerItemBinding.address = bean.province + bean.city + bean.district + bean.place

    }

    constructor(context: Context) : super(context)

    var edit: ((e: UserAddress.Address) -> Unit)? = null
    var del: ((e: UserAddress.Address) -> Unit)? = null
    var itemClick: ((e: UserAddress.Address) -> Unit)? = null
    var defItem: ((e: UserAddress.Address) -> Unit)? = null

    fun runEdit(e: UserAddress.Address) {
        edit?.let {
            it(e)
        }
    }

    fun runDel(e: UserAddress.Address) {
        del?.let {
            it(e)
        }

    }

    fun runItemClick(e: UserAddress.Address) {
        itemClick?.let {
            it(e)
        }
    }

    fun runDefItem(e: UserAddress.Address) {
        defItem?.let {
            it(e)
        }
    }
}